var app = angular.module('app', ['infinite-scroll', 'wu.masonry']);

app.controller('ProductsController', function ($scope, ProductsView, CartView, $http) {
    $scope.productsView = new ProductsView();
    $scope.productCart = new CartView();
});

app.factory('CartView', function ($http) {
    var CartView = function () {
        this.nbProduct = 0;
        this.products = [];
        this.totalPrice = 0;
        this.display = 'hide';
        this.url = HTTPNAME + "/index.php/manipulatecart";
        $http({
            url: this.url,
            data: '',
            method: 'POST',
            headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
        }).success(function (data) {
                this.successUpdate(data);
                $(function () {
                    $('#panier').stop().animate({
                        right: '0px'
                    }, 1000, function () {
                        $('#panier').stop().animate({
                            right: '-262px'
                        }, 1000);
                    });
                });
            }.bind(this));
    };

    CartView.prototype.addCart = function (product) {
        this.updateCart(product, 1, 0);
    };
    CartView.prototype.removeCart = function (product) {
        this.updateCart(product, 1, 0);
    };
    CartView.prototype.successUpdate = function (data, product, qty) {
        this.nbProduct = data.productsQty;
        this.totalPrice = data.totalPrice;
        var products = [];
        if (data.products != undefined) {
            jQuery.each(data.products, function (i, val) {
                products.push(val);
            });
        }

        this.products = products;

        if (this.totalPrice == 0 || this.totalPrice == undefined) {
            this.display = 'hide';
        } else {
            this.display = '';
            if (product == undefined || qty == undefined) {
                return;
            }
            qty = parseInt(qty);
            if (qty > 0) {
                $.gritter.add({
                    title: 'Add cart',
                    text: qty + ' ' + product.title + ' has been added'
                });
            } else {
                qty = qty * -1;
                $.gritter.add({
                    title: 'Remove cart',
                    text: qty + ' ' + product.title + ' has been removed'
                });
            }

        }
    };
    CartView.prototype.updateCart = function (product, qty, force) {
        product.qty = qty;
        product.force = force;
        $http({
            url: this.url,
            data: $.param(product),
            method: 'POST',
            headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
        }).success(function (data) {
                this.successUpdate(data, product, qty);
            }.bind(this));
    };

    return CartView;
});
app.factory('ProductsView', function ($http, $filter) {
    var ProductsView = function () {
        this.products = [];
        this.productsOrig = [];
        this.startPrice = 0;
        this.endPrice = 0;
        this.startQty = 0;
        this.endQty = 0;
        this.filtername = '';
        this.busy = false;
        this.after = 0;

    };

    ProductsView.prototype.nextPage = function () {
        if (this.busy) return;
        this.busy = true;

        var url = HTTPNAME + "/index.php/productview-" + this.after;

        $http.get(url).success(function (data) {
            var products = data;
            for (var i = 0; i < products.length; i++) {

                this.productsOrig.push(products[i]);
            }
            if (this.products == undefined || this.products.length == 0) {
                this.products = this.productsOrig;
            }
            this.filterName(this.filtername, true);
            this.filterPrice(this.startPrice, this.endPrice, true);
            this.filterQty(this.startQty, this.endQty, true);
            var nextAfter = this.after + products.length;
            if (this.after == nextAfter) {
                return;
            }
            this.after = nextAfter;
            this.busy = false;
            // init

        }.bind(this));

    };
    ProductsView.prototype.filterName = function (name, stopFilter, useGlobalList) {
        if (useGlobalList == undefined) {
            useGlobalList = false;
        }
        var productItems;
        if (stopFilter == undefined || stopFilter == false || useGlobalList) {
            productItems = this.productsOrig;
        } else {
            productItems = this.products;
        }
        if ((name == undefined || name == '') && (this.filtername != undefined && this.filtername != '')) {
            this.products = productItems;
            this.filtername = '';
            var globalList = true;
            if (this.startPrice > 0) {
                this.filterPrice(this.startPrice, this.endPrice, true, globalList);
                globalList = false;
            }
            if (this.startQty > 0) {
                this.filterQty(this.startQty, this.endQty, true, globalList);
                globalList = false;
            }
            return;
        } else if (name == undefined || name == '') {
            return;
        }
        this.filtername = name;
        this.products = $filter('filter')(productItems, {"title": name});
        if (stopFilter == undefined || stopFilter == false) {
            this.filterPrice(this.startPrice, this.endPrice, true);
            this.filterQty(this.startQty, this.endQty, true);
        }

    };
    ProductsView.prototype.filterPrice = function (start, end, stopFilter, useGlobalList) {
        if (useGlobalList == undefined) {
            useGlobalList = false;
        }
        var productItems;
        if (stopFilter == undefined || stopFilter == false || useGlobalList) {
            productItems = this.productsOrig;
        } else {
            productItems = this.products;
        }

        if (start == undefined || start < 0 || start == '') {
            start = 0;
        }
        if (start == 0 && this.startPrice > 0) {
            this.products = productItems;
            this.startPrice = 0;
            this.endPrice = 0;
            var globalList = true;
            if (this.filtername != undefined && this.filtername != '') {
                this.filterName(this.filtername, true, globalList);
                globalList = false;
            }
            if (this.startQty > 0) {
                this.filterQty(this.startQty, this.endQty, true, globalList);
                globalList = false;
            }
            return;
        } else if (start == 0) {
            return;
        }
        this.startPrice = start;
        this.endPrice = end;
        productItems = $filter('filter')(productItems, {"price": start}, function (actual, expected) {
            actual = parseFloat(actual);
            expected = parseFloat(expected);
            if (actual >= expected) {
                return true;
            }
            return false;
        });
        productItems = $filter('filter')(productItems, {"price": end}, function (actual, expected) {
            if (expected == undefined || expected == '' || expected == 0) {
                return true;
            }
            actual = parseFloat(actual);
            expected = parseFloat(expected);
            if (actual <= expected) {
                return true;
            }
            return false;
        });
        this.products = productItems;
        if (stopFilter == undefined || stopFilter == false) {
            this.filterName(this.filtername, true);
            this.filterQty(this.startQty, this.endQty, true);
        }
    };
    ProductsView.prototype.filterQty = function (start, end, stopFilter, useGlobalList) {
        if (useGlobalList == undefined) {
            useGlobalList = false;
        }
        var productItems;
        if (stopFilter == undefined || stopFilter == false || useGlobalList) {
            productItems = this.productsOrig;
        } else {
            productItems = this.products;
        }

        if (start == undefined || start < 0 || start == '') {
            start = 0;
        }
        if (start == 0 && this.startQty > 0) {
            this.products = productItems;
            this.startQty = 0;
            this.endQty = 0;
            var globalList = true;
            if (this.filtername != undefined && this.filtername != '') {
                this.filterName(this.filtername, true, globalList);
                globalList = false;
            }
            if (this.startPrice > 0) {
                this.filterPrice(this.startPrice, this.endPrice, true, globalList);
                globalList = false;
            }

            return;
        } else if (start == 0) {
            return;
        }
        this.startQty = start;
        this.endQty = end;
        productItems = $filter('filter')(productItems, {"number": start}, function (actual, expected) {
            actual = parseInt(actual);
            expected = parseInt(expected);
            if (actual >= expected) {
                return true;
            }
            return false;
        });
        productItems = $filter('filter')(productItems, {"number": end}, function (actual, expected) {
            if (expected == undefined || expected == '' || expected == 0) {
                return true;
            }
            actual = parseInt(actual);
            expected = parseInt(expected);
            if (actual <= expected) {
                return true;
            }
            return false;
        });
        this.products = productItems;
        if (stopFilter == undefined || stopFilter == false) {
            this.filterName(this.filtername, true);
            this.filterPrice(this.startPrice, this.endPrice, true);
        }
    };

    return ProductsView;
});